<?php
class ModelArticleArticle extends Model {
    public function updateViewed($article_id) {
		$this->db->query("UPDATE " . DB_PREFIX . "article SET viewed = (viewed + 1) WHERE article_id = '" . (int)$article_id . "'");
	}
    
	public function getArticle($article_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "article a LEFT JOIN " . DB_PREFIX . "article_description ad ON (a.article_id = ad.article_id)  WHERE a.article_id = '" . (int)$article_id . "' AND a.status = '1'");
		
		return $query->row;
	}
	public function getArticleDescriptions($article_id) {
		$article_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "article_description WHERE article_id = '" . (int)$article_id . "'");
		
		foreach ($query->rows as $result) {
			$article_description_data = array(
				'content'          => $result['content'],
				'meta_keyword'     => $result['meta_keyword']
			);
		}
		
		return $article_description_data;
	}	
    
    public function getArticles($data = array()) {
        		
        $cache = md5(http_build_query($data));
		$article_data = $this->cache->get('article.' . $cache);
        if (!$article_data) {
            $sql = "SELECT DISTINCT * FROM " . DB_PREFIX . "article a LEFT JOIN " . DB_PREFIX . "article_description ad ON (a.article_id = ad.article_id)  WHERE a.status = '1'";
                    
            if (isset($data['filter_category_id']) && $data['filter_category_id']) {
				$implode_data = array();
				
				$this->load->model('article/category');
				$categories = $this->model_article_category->getCategoriesByParentId($data['filter_category_id']);
				
				foreach ($categories as $category_id) {
					$implode_data[] = "'".(int)$category_id ."'";
				}
				
				$sql .= " AND a.article_id IN (" .implode(' , ', $implode_data) . ")";	
            }
        
            $query = $this->db->query($sql);
            foreach ($query->rows as $result) {
				$article_data[$result['article_id']] = $this->getArticle($result['article_id']);
			}
            $this->cache->set('article.' . $cache, $article_data);
		}
		
		return $article_data;
    }
    public function getTotalArticles($data=array()) {
        $sql = "SELECT COUNT(DISTINCT a.article_id) AS total FROM " . DB_PREFIX . "article a LEFT JOIN " . DB_PREFIX . "article_description ad ON (a.article_id = ad.article_id)  WHERE a.status = '1'";
        
            if (isset($data['filter_category_id']) && $data['filter_category_id']) {
				$implode_data = array();
				
				$this->load->model('article/category');
				$categories = $this->model_article_category->getCategoriesByParentId($data['filter_category_id']);
				
				foreach ($categories as $category_id) {
					$implode_data[] = "'".(int)$category_id ."'";
				}
				
				$sql .= " AND a.article_id IN (" .implode(' , ', $implode_data) . ")";	
            }
            
        $query = $this->db->query($sql);
		return $query->row['total'];
        
	}
    
    public function getTopArticle($num=5)
    {
        $sql = "SELECT DISTINCT * FROM " . DB_PREFIX . "article a LEFT JOIN " . DB_PREFIX . "article_description ad ON (a.article_id = ad.article_id)  WHERE a.status = '1' and a.hot = '1' ORDER BY date_modified DESC LIMIT $num ";
        
        $query = $this->db->query($sql);
		return $query->rows;
    }
}
?>